package com.hg.library.config;

import android.content.Context;
import android.graphics.Bitmap;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/**
 * Created by ASUS-PC on 2016/1/18.
 */
public class ImageLoaderConfig {

    public static void build(Context context) {

        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context)
                .memoryCacheSize(50 * 1024 * 1024)
                .diskCacheFileCount(500)
                .memoryCache(new LruMemoryCache(50 * 1024 * 1024))
                .diskCacheFileNameGenerator(new Md5FileNameGenerator());

        DisplayImageOptions.Builder options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)                          //启用内存缓存
                .cacheOnDisk(true)                             //启用外存缓存
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .delayBeforeLoading(500)
                .resetViewBeforeLoading(false);

        config.defaultDisplayImageOptions(options.build());

        ImageLoader.getInstance().init(config.build());
    }

    public static DisplayImageOptions showDisplayImage(int showImageDefault){
        return showDisplayImage(showImageDefault,showImageDefault);
    }

    public static DisplayImageOptions showDisplayImage(int showImageOnLoading,int showImageOnFail){
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)                          //启用内存缓存
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .delayBeforeLoading(500)
                .resetViewBeforeLoading(false)
                .showImageOnLoading(showImageOnLoading)
                .showImageOnFail(showImageOnFail)
//                .displayer(new FadeInBitmapDisplayer(500))
                .build();
        return options;
    }

    public static DisplayImageOptions showDisplayAnim(int showImageOnLoading, BitmapDisplayer bitmapDisplayer){
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)                          //启用内存缓存
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .delayBeforeLoading(500)
                .resetViewBeforeLoading(false)
                .showImageOnLoading(showImageOnLoading)
                .showImageOnFail(showImageOnLoading)
                .displayer(bitmapDisplayer)
                .build();
        return options;
    }
}
